package com.woniu.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.backstage.common.PageResult;
import com.woniu.backstage.dao.TbInformMapper;
import com.woniu.backstage.entity.TbInform;
import com.woniu.backstage.entity.TbUser;
import com.woniu.backstage.entity.form.InformForm;
import com.woniu.backstage.service.ITbInformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author spirit
 * @since 2020-04-18
 */
@Service
public class TbInformServiceImpl extends ServiceImpl<TbInformMapper, TbInform> implements ITbInformService {

    @Autowired
    private TbInformMapper informMapper;

    @Override
    public PageResult getAllInform(Integer currentPage, Integer pageSize) {
        //查询出总的数据条数
        Integer total = informMapper.getTotal();
        //计算出开始的数据
        int begin = (currentPage-1)*pageSize;
        //查询出数据
        List<TbInform> informList = informMapper.getAllInform(begin, pageSize);
        PageResult pageResult = new PageResult(0,"success",informList,currentPage,total);
        return pageResult;
    }

    @Override
    public IPage<TbInform> pageLimit(Page<TbInform> page, InformForm informForm) {
        QueryWrapper<TbInform> wrapper = new QueryWrapper<>();
        if(informForm.getTitle() != null && informForm.getTitle().length() > 0){
            System.out.println("啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
            wrapper.eq("title",informForm.getTitle());
        }
        if(informForm.getStartTime() != null){
            wrapper.ge("create_time",informForm.getStartTime());
        }
        if(informForm.getEndTime() != null){
            wrapper.le("create_time",informForm.getEndTime());
        }
        wrapper.eq("type","系统提醒");
        IPage<TbInform> informIPage = informMapper.selectPage(page, wrapper);
        return informIPage;
    }

    @Override
    public int addInform(TbInform inform) {
        return informMapper.insert(inform);
    }

    @Override
    public int deleteInform(Serializable id) {
        return informMapper.deleteById(id);
    }

    @Override
    public Integer deleteBatch(List<Integer> idArr) {
        return informMapper.deleteBatch(idArr);
    }

}
